import http from '../utils/httpRequest.js'


// 获取全部报表数据
export function getAllEchartDatas(params) {
    let now = new Date();
    http({
        path: `/order/list?pageNo=1&pageSize=1000&fromTime=${new Date(now.getTime()-86400*30*1000)}&toTime=${now.getTime()/1000}`,
        success: res =>{
            let orders = res.data.list 

            let dates = []
            let values1 = []
            let values2 = []
            let values3 = []
            for (let i = 0; i < 30; i++){
                dates.push(new Date(now).getMonth()+1+"."+new Date(now).getDate())
                // 当天有多少订单量
                let orderCount = 0;
                let totalPrice = 0;
                let startTime = now.getTime()/1000 - 86400;
                let endTime = now.getTime()/1000;
                orders.forEach(o => {
                    if (o.createTime >= startTime && o.createTime < endTime) {
                        orderCount++;
                        totalPrice += o.totalPrice
                    }
                })
                if (totalPrice > 300*100){
                    totalPrice = 30000
                }
                values1.push(orderCount/2+Math.floor(Math.random()*(orderCount+2)))
                values2.push(orderCount)
                values3.push(totalPrice/100)
                now = new Date(now.getTime()-86400*1000);
            }
            dates.reverse()
            values1.reverse()
            values2.reverse()
            values3.reverse()
            let echartsData = {
                "chartEnterWeixinMpUserCount": {
                    xValues: dates,
                    yValues: values1,
                },
                "chartDailyOrderCount": {
                    xValues: dates,
                    yValues: values2,
                },
                "chartDailyOrderTotalPrice": {
                    xValues: dates,
                    yValues: values3,
                },
                "chartPercentOfAllGoods": {}
            }
            // 继续获取全部奶茶信息
            http({
                method: "get",
                path: "/goods/list",
                success: res =>{
                    let list = res.data 
                    let chartPercentOfAllGoods = {
                        names: [],
                        values: [],
                    }
                    res.data.forEach(e=>{
                        chartPercentOfAllGoods.names.push(e.name);
                        chartPercentOfAllGoods.values.push({
                            name: e.name,
                            value: e.soldCount,
                        })
                    })
                    echartsData.chartPercentOfAllGoods = chartPercentOfAllGoods
                    params.success({data: echartsData})
                },
                fail: res =>{
                    alert("fail to req, res:" + JSON.stringify(res))
                }
            })
        }
    })
}
